#include "player.h"

#include <QtGui>

using namespace Playur;

Playur::Player::Player()
{
    setWindowTitle("Playur Player");
    QWidget* centralWidget = new QWidget(this);
    QVBoxLayout* layout = new QVBoxLayout;

    layout->addWidget(label = new QLabel(tr("Drop a track here to play it")));
    layout->addWidget(button = new QPushButton(tr("Stop")));
    button->setEnabled(false);

    centralWidget->setLayout(layout);
    setCentralWidget(centralWidget);
    setAcceptDrops(true);

    audioOutput = new Phonon::AudioOutput(Phonon::MusicCategory, this);
    mediaObject = new Phonon::MediaObject(this);
    Phonon::createPath(mediaObject, audioOutput);

    connect(button, SIGNAL(clicked()), this, SLOT(buttonClicked()));
}

void Player::buttonClicked()
{
    mediaObject->stop();
    button->setEnabled(false);
}

void Playur::Player::dragEnterEvent(QDragEnterEvent* event)
{
    event->acceptProposedAction();
}

void Playur::Player::dropEvent(QDropEvent* event)
{
    qDebug() << "Playing" << event->mimeData()->text();
    mediaObject->setCurrentSource(event->mimeData()->text());
    mediaObject->play();

    label->setText(QString(tr("Now playing %1")).arg(event->mimeData()->text()));
    button->setEnabled(true);

    event->acceptProposedAction();
}
